/*
 * Name: $RCSfile: AsyncHttpResponseProcess.java,v $
 * Version: $Revision: 1.1 $
 * Date: $Date: Jan 16, 2012 11:18:03 AM $
 *
 * Copyright (C) 2011 COMPANY_NAME, Inc. All rights reserved.
 */

package son.hut.vn.app.network;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import son.hut.vn.app.R;
import son.hut.vn.app.infor.WebserviceConfig;
import son.hut.vn.app.json.CommonInfo;
import son.hut.vn.app.ui.BaseActivity;
import son.hut.vn.app.utility.SmartLogUtility;
import android.text.TextUtils;

/**
 * @author sonnv6207
 */
public class AsyncHttpResponseProcess implements AsyncHttpResponseListener
{

    private static final String TAG = "AsyncHttpResponseProcess";

    private BaseActivity context;

    private boolean isShowDialog = true;
    
    public AsyncHttpResponseProcess(BaseActivity context)
    {
        this.context = context;
    }

    
    /**
     * @param context
     * @param isShowDialog
     */
    public AsyncHttpResponseProcess(BaseActivity context, boolean isShowDialog)
    {
        this.context = context;
        this.isShowDialog = isShowDialog;
    }
    
    @Override
    public void before()
    {
        if(isShowDialog){
            // Show waiting dialog during connection
            context.showProgressDialog();
        }
    }

    @Override
    public void after(int statusCode, HttpResponse response)
    {
        if(isShowDialog){
            // Process server response
            context.closeProgressDialog();
        }
        switch (statusCode)
        {
            case AsyncHttpBase.NETWORK_STATUS_TIMEOUT:
                context.showCustomPositiveDialog(R.string.dialog_title_error,R.drawable.icon_error,R.string.message_request_time_out,R.drawable.button_ok);
                break;
            case AsyncHttpBase.NETWORK_STATUS_OFF:
                context.showCustomPositiveDialog(R.string.dialog_title_error,R.drawable.icon_error,R.string.message_network_is_unavailable, R.drawable.button_ok);
                break;
            case AsyncHttpBase.NETWORK_STATUS_ERROR:
                context.showCustomPositiveDialog(R.string.dialog_title_error,R.drawable.icon_error, R.string.message_network_error, R.drawable.button_ok);
                break;
            case AsyncHttpBase.NETWORK_EXCEPTION:
                String error = AsyncHttpBase.CAUSE_ERROR;
                if(TextUtils.isEmpty(error)){
                    error = context.getResources().getString(R.string.message_server_error);
                }
                
                context.showCustomPositiveDialog(R.string.dialog_title_error, R.drawable.icon_error,error, R.drawable.button_ok);
                break;
            case AsyncHttpBase.NETWORK_STATUS_OK:
                processHttpResponse(response);
                break;
            default:
                context.showCustomPositiveDialog(R.string.dialog_title_error, R.drawable.icon_error, R.string.message_server_error, R.drawable.button_ok);
        }
    }

    /**
     * Process HttpResponse
     * 
     * @param response
     */
    public void processHttpResponse(HttpResponse response)
    {
        String json = "";
        try
        {
            // Get json response
            json = EntityUtils.toString(response.getEntity());
            if (json == null)
            {
                context.showCustomPositiveDialog(R.string.dialog_title_error, R.drawable.icon_error, R.string.message_error_cant_extract_server_response, R.drawable.button_ok);
                return;
            }

            // Parser information
            SmartLogUtility.log(TAG, "Server response string/commonInfo:" + json);
            
            CommonInfo commonInfo = new CommonInfo();
            JSONObject entry = new JSONObject(json);
            commonInfo.setSuccess(entry.getInt(WebserviceConfig.KEY_SUCCESS));
            SmartLogUtility.log(TAG, "commonInfo getSuccess: "+ commonInfo.getSuccess());
            if (commonInfo.getSuccess() == 1)
            {
                commonInfo.setStatus(entry.getInt(WebserviceConfig.KEY_STATUS));
                processIfResponseSuccess(json);
            }
            else
            {
                commonInfo.setMessage(entry.getString(WebserviceConfig.KEY_MESSAGE));
                processIfResponseFail();
                context.checkInvalidToken(commonInfo.getMessage());
            }
        }
        catch (Exception e)
        {
            context.showCustomPositiveDialog(R.string.dialog_title_error,R.drawable.icon_error, R.string.message_server_error,R.drawable.button_ok);
            e.printStackTrace();
        }
    }

    /**
     * Interface function
     */
    public void processIfResponseSuccess(String response)
    {
         SmartLogUtility.log(TAG, "Process if response is success ===================");
    }

    /**
     * Interface function
     */
    public void processIfResponseFail()
    {
         SmartLogUtility.log(TAG, "Process if response is fail ===================");
    }
    
}
